package org.tloss.web.editor.form.editors;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;
import org.tloss.web.editor.Utils;
import org.tloss.web.editor.enity.editors.JavaClassPropertyDescriptor;
import org.tloss.web.editor.model.FormField;

public class FormFieldProperties implements IPropertySource {
	private FormField field;
	private IResourceChangeListener dirtyListener;
	private IPropertyDescriptor[] descriptors;
	public static final String TYPE_PRO = "TYPE_PRO";
	public static final String MAPPING_TYPE_PRO = "MAPPING_TYPE_PRO";
	public static final String MAX_LENGTH_PRO = "MAX_LENGTH_PRO";
	public static final String DISABLED_PRO = "DISABLED_PRO";
	public static final String REQUIRED_PRO = "REQUIRED_PRO";
	public static final String WEIGHT_PRO = "WEIGHT_PRO";
	public static final String TITLE_PRO = "TITLE_PRO";

	List<String> supportedType = new ArrayList<String>();
	String[] supportedTypeArray = null;

	public void initSupportedType() {
		supportedType.add("text_format");
		supportedType.add("textarea");
		supportedType.add("textfield");
		supportedType.add("password");
		supportedType.add("checkbox");
		supportedType.add("checkboxes");
		supportedType.add("date");
		supportedType.add("radio");
		supportedType.add("radios");
		supportedType.add("select");
		supportedType.add("submit");

		supportedTypeArray = new String[supportedType.size()];
		supportedType.toArray(supportedTypeArray);
	}

	public FormFieldProperties(FormField field,
			IResourceChangeListener dirtyListener) {
		this.field = field;
		this.dirtyListener = dirtyListener;
		descriptors = new IPropertyDescriptor[7];
		initSupportedType();
		descriptors[0] = new ComboBoxPropertyDescriptor(TYPE_PRO, "Type",
				supportedTypeArray);
		descriptors[1] = new TextPropertyDescriptor(MAX_LENGTH_PRO,
				"Max Length");
		descriptors[2] = new ComboBoxPropertyDescriptor(DISABLED_PRO,
				"Disabled", new String[] { "Yes", "No" });
		descriptors[3] = new ComboBoxPropertyDescriptor(REQUIRED_PRO,
				"Required", new String[] { "Yes", "No" });
		descriptors[4] = new TextPropertyDescriptor(WEIGHT_PRO, "Weight");
		descriptors[5] = new TextPropertyDescriptor(TITLE_PRO, "Title");
		descriptors[6] = new JavaClassPropertyDescriptor(MAPPING_TYPE_PRO,
				"Mapping Type");

	}

	@Override
	public Object getEditableValue() {
		return this;
	}

	@Override
	public IPropertyDescriptor[] getPropertyDescriptors() {

		return descriptors;
	}

	@Override
	public Object getPropertyValue(Object arg0) {
		if (TYPE_PRO.equals(arg0)) {
			int index = supportedType.indexOf(field.getType());
			if (index != -1) {
				return index;
			}
			return 0;
		} else if (MAX_LENGTH_PRO.equals(arg0)) {
			return String.valueOf(field.getMaxLength());
		} else if (DISABLED_PRO.equals(arg0)) {
			if (field.getDisabled()) {
				return 0;
			} else
				return 1;
		} else if (REQUIRED_PRO.equals(arg0)) {
			if (field.getRequired()) {
				return 0;
			} else
				return 1;
		} else if (WEIGHT_PRO.equals(arg0)) {
			return String.valueOf(field.getWeight());
		} else if (TITLE_PRO.equals(arg0)) {
			return field.getTitle() != null ? field.getTitle() : "";
		} else if (MAPPING_TYPE_PRO.equals(arg0)) {
			return field.getMappingType() != null ? field.getMappingType()
					: "java.lang.String";
		}
		return "";
	}

	@Override
	public boolean isPropertySet(Object arg0) {
		return false;
	}

	@Override
	public void resetPropertyValue(Object arg0) {

	}

	@Override
	public void setPropertyValue(Object arg0, Object arg1) {
		if (TYPE_PRO.equals(arg0)) {
			field.setType(supportedType.get((Integer) arg1));

		} else if (MAX_LENGTH_PRO.equals(arg0)) {
			try {
				field.setMaxLength(Integer.parseInt((String) arg1));
			} catch (Exception e) {
				Utils.logError("Legnth is invalid", e);
			}
		} else if (DISABLED_PRO.equals(arg0)) {
			if (Integer.valueOf(0).equals(arg1)) {
				field.setDisabled(true);
			} else
				field.setDisabled(false);
		} else if (REQUIRED_PRO.equals(arg0)) {
			if (Integer.valueOf(0).equals(arg1)) {
				field.setRequired(true);
			} else
				field.setRequired(false);
		} else if (WEIGHT_PRO.equals(arg0)) {
			try {
				field.setWeight(Integer.parseInt((String) arg1));
			} catch (Exception e) {
				Utils.logError("Weight is invalid", e);
			}
		} else if (TITLE_PRO.equals(arg0)) {
			field.setTitle((String) arg1);
		}  else if (MAPPING_TYPE_PRO.equals(arg0)) {
			field.setMappingType(arg1 !=null ? arg1.toString(): "java.lang.String");
		}
		if (dirtyListener != null) {
			dirtyListener.resourceChanged(null);
		}
	}
}
